package com.example.state;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.example.bounce.Bounce;
import com.example.bounce.R;
import com.example.glsurfaceview.MyGLSurfaceView;
import com.example.glutil.WorldCoord;
import com.example.object.screen.BallShape;
import com.example.object.screen.Locker;

import org.jbox2d.common.Vec2;

/**
 * Created by Lucas on 06/06/13.
 */
public class Ball extends AppState {

    /**
     * very easy difficulty
     */
    public final int VERYEASY = 1;
    /**
     * easy difficulty
     */
    public final int EASY = 2;
    /**
     * medium difficulty
     */
    public final int MEDIUM = 3;
    /**
     * hard difficulty
     */
    public final int HARD = 4;
    /**
     * very hard difficulty
     */
    public final int VERYHARD = 5;
    /**
     * no difficulty
     */
    public final int NONE = 0;
    /**
     * The debug TAG
     */
    private final String TAG = "BallState";

    /**
     * The constructor
     * @param context the context of the application
     * @param glView the glSurfaceView
     */
    public Ball(Context context, MyGLSurfaceView glView) {
        super(context, glView);
        _bounce = (Bounce)context;
    }

    /**
     * initialize the list of game shape
     */
    public void initGameShape(){
        Log.i(TAG, "Initialize game's shapes");
        addLocker();
        addBall();

    }

    /**
     * initialize the locker in the list of game shape
     */
    private void addLocker(){
        _listGameShape.add(new Locker(_context, 0.75f*AppState.SIMULATION_HEIGHT, 1f/3f, true, VERYEASY));
        _listGameShape.add(new Locker(_context, 0.75f*AppState.SIMULATION_HEIGHT, 1f, true, EASY));
        _listGameShape.add(new Locker(_context, 0.75f*AppState.SIMULATION_HEIGHT, 5f/3f, true, MEDIUM));
        _listGameShape.add(new Locker(_context, 0.25f*AppState.SIMULATION_HEIGHT, 1f/3f, true, HARD));
        _listGameShape.add(new Locker(_context, 0.25f*AppState.SIMULATION_HEIGHT, 1f, true, VERYHARD));
        _listGameShape.add(new Locker(_context, 0.25f*AppState.SIMULATION_HEIGHT, 5f/3f, false, NONE));
    }

    /**
     * initialize the ball in the list of game shape
     */
    private void addBall(){
        _listGameShape.add(new BallShape(_context, 0.54f*AppState.SIMULATION_HEIGHT, 1f/3f, 0.095f, 0.095f, R.raw.baseball));
        _listGameShape.add(new BallShape(_context, 0.57f*AppState.SIMULATION_HEIGHT, 1f,    0.19f, 0.19f, R.raw.soccerball));
        _listGameShape.add(new BallShape(_context, 0.54f*AppState.SIMULATION_HEIGHT, 5f/3f, 0.095f, 0.095f, R.raw.tennisball));
        _listGameShape.add(new BallShape(_context, 0.035f*AppState.SIMULATION_HEIGHT, 1f/3f, 0.08f, 0.08f, R.raw.pingpongball));
        _listGameShape.add(new BallShape(_context, 0.051f*AppState.SIMULATION_HEIGHT, 1f,   AppState.SIMULATION_HEIGHT *0.04f, AppState.SIMULATION_WIDTH *0.1f, R.raw.football));
    }

    /**
     * Handle the input event
     * @param e the Motion event to handle
     * @return true if the event is treated
     */
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // convert into the coordinates of the object of the screen
        if (e.getAction() == MotionEvent.ACTION_DOWN){
            Vec2 touch = new Vec2(e.getX(), AppState.DEVICE_HEIGHT - e.getY());
            touch = WorldCoord.getCoordinates(_bounce, touch);
            for (int i=0; i < _listGameShape.size(); i++){
                if(_listGameShape.get(i).isTouchable()){
                    if (_listGameShape.get(i).isTouched(touch)){
                        _bounce.setDifficulty(_listGameShape.get(i).getUserData());
                        _bounce.changeState(WORLD);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * handle the key down event for the back button
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            _bounce.changeState(HOME);
            return false;
        }
        return false;
    }
}
